home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / mmalloc / mmap-sup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-01  |  4.0 KB  |  137 lines

  1. /* Support for an sbrk-like function that uses mmap.
  2.    Copyright 1992 Free Software Foundation, Inc.
  3.  
  4.    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #if defined(HAVE_MMAP)
  21.  
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #include <sys/mman.h>
  26.  
  27. #ifndef SEEK_SET
  28. #define SEEK_SET 0
  29. #endif
  30.  
  31. #include "mmalloc.h"
  32.  
  33. /* Cache the pagesize for the current host machine.  Note that if the host
  34.    does not readily provide a getpagesize() function, we need to emulate it
  35.    elsewhere, not clutter up this file with lots of kluges to try to figure
  36.    it out. */
  37.  
  38. static size_t pagesize;
  39.  
  40. #define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + pagesize - 1) & \
  41.                     ~(pagesize - 1))
  42.  
  43. /*  Get core for the memory region specified by MDP, using SIZE as the
  44.     amount to either add to or subtract from the existing region.  Works
  45.     like sbrk(), but using mmap(). */
  46.  
  47. PTR
  48. __mmalloc_mmap_morecore (mdp, size)
  49.   struct mdesc *mdp;
  50.   int size;
  51. {
  52.   PTR result = NULL;
  53.   off_t foffset;    /* File offset at which new mapping will start */
  54.   size_t mapbytes;    /* Number of bytes to map */
  55.   caddr_t moveto;    /* Address where we wish to move "break value" to */
  56.   caddr_t mapto;    /* Address we actually mapped to */
  57.   char buf = 0;        /* Single byte to write to extend mapped file */
  58.  
  59.   if (pagesize == 0)
  60.     {
  61.       pagesize = getpagesize ();
  62.     }
  63.   if (size == 0)
  64.     {
  65.       /* Just return the current "break" value. */
  66.       result = mdp -> breakval;
  67.     }
  68.   else if (size < 0)
  69.     {
  70.       /* We are deallocating memory.  If the amount requested would cause
  71.      us to try to deallocate back past the base of the mmap'd region
  72.      then do nothing, and return NULL.  Otherwise, deallocate the
  73.      memory and return the old break value. */
  74.       if (mdp -> breakval + size >= mdp -> base)
  75.     {
  76.       result = (PTR) mdp -> breakval;
  77.       mdp -> breakval += size;
  78.       moveto = PAGE_ALIGN (mdp -> breakval);
  79.       munmap (moveto, (size_t) (mdp -> top - moveto));
  80.       mdp -> top = moveto;
  81.     }
  82.     }
  83.   else
  84.     {
  85.       /* We are allocating memory.  Make sure we have an open file
  86.      descriptor and then go on to get the memory. */
  87.       if (mdp -> fd < 0)
  88.     {
  89.       result = NULL;
  90.     }
  91.       else if (mdp -> breakval + size > mdp -> top)
  92.     {
  93.       /* The request would move us past the end of the currently
  94.          mapped memory, so map in enough more memory to satisfy
  95.          the request.  This means we also have to grow the mapped-to
  96.          file by an appropriate amount, since mmap cannot be used
  97.          to extend a file. */
  98.       moveto = PAGE_ALIGN (mdp -> breakval + size);
  99.       mapbytes = moveto - mdp -> top;
  100.       foffset = mdp -> top - mdp -> base;
  101.       /* FIXME:  Test results of lseek() and write() */
  102.       lseek (mdp -> fd, foffset + mapbytes - 1, SEEK_SET);
  103.       write (mdp -> fd, &buf, 1);
  104.       mapto = mmap (mdp -> top, mapbytes, PROT_READ | PROT_WRITE,
  105.             MAP_SHARED | MAP_FIXED, mdp -> fd, foffset);
  106.       if (mapto == mdp -> top)
  107.         {
  108.           mdp -> top = moveto;
  109.           result = (PTR) mdp -> breakval;
  110.           mdp -> breakval += size;
  111.         }
  112.     }
  113.       else
  114.     {
  115.       result = (PTR) mdp -> breakval;
  116.       mdp -> breakval += size;
  117.     }
  118.     }
  119.   return (result);
  120. }
  121.  
  122. PTR
  123. __mmalloc_remap_core (mdp)
  124.   struct mdesc *mdp;
  125. {
  126.   caddr_t base;
  127.  
  128.   /* FIXME:  Quick hack, needs error checking and other attention. */
  129.  
  130.   base = mmap (mdp -> base, mdp -> top - mdp -> base,
  131.            PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
  132.            mdp -> fd, 0);
  133.   return ((PTR) base);
  134. }
  135.  
  136. #endif    /* defined(HAVE_MMAP) */
  137.